from typing import Optional, List

from fastapi import Query, Header, Depends, Body, Request
from fastapi_utils.cbv import cbv
from fastapi_utils.inferring_router import InferringRouter
from requests import Session

from server.app.cloud_ide.model.ide_stack import IdeStack
from server.app.cloud_ide.schema.ide_stack.request.ide_stack_create_request import IdeStackCreateRequest
from server.app.cloud_ide.schema.ide_stack.request.ide_stack_update_request import IdeStackUpdateRequest
from server.app.cloud_ide.service.ide_stack_service import IdeStackService
from server.framework.core.http_schemas import TableResponseSchema, CommonResponseSchema
from server.framework.core.injects import get_db
from server.framework.core.schemas import OptionsSchema

ide_stack_api = InferringRouter()


@cbv(ide_stack_api)
class IdeStackApi:
    ide_stack_service = IdeStackService()
    db: Session = Depends(get_db)

    @ide_stack_api.get("/list", response_model=TableResponseSchema, name='获取技术栈列表')
    async def list(
            self,
            group: Optional[str] = Header(None),
            name: Optional[str] = Query(None, description='过滤条件:ide名称'),
            current: Optional[int] = Query(1, description='当前页数'),
            page_size: Optional[int] = Query(10, description='每页记录的数量', alias='pageSize')) -> TableResponseSchema:
        """
        获取技术栈列表
        :return:
        """
        items, total = self.ide_stack_service.get_ide_stack_list(self.db, group, name, page_size, current)
        return TableResponseSchema(data=items, total=total, page=page_size, success=True)

    @ide_stack_api.get("/get_entity", name='获取技术栈实体')
    async def get_entity(self, id: str = Query(None, description='技术栈id')):
        entity = self.ide_stack_service.get_ide_stack_by_id(self.db, id)
        return entity

    @ide_stack_api.post("/create", response_model=CommonResponseSchema, name='创建技术栈')
    async def create(self, request: Request,
                     data: IdeStackCreateRequest = Body(None, description='IDE创建实体'),
                     group: Optional[str] = Header(None)) -> CommonResponseSchema:
        """
        创建技术栈
        :return:
        """
        data.tenant_id = group
        rs = self.ide_stack_service.create_ide_stack(self.db, request.state.user_name, request.state.user_id,
                                                     data)
        if rs:
            return CommonResponseSchema(data={}, message=f'创建成功', success=True)
        else:
            return CommonResponseSchema(data={}, message=f'创建失败', success=False)

    @ide_stack_api.get("/delete", response_model=CommonResponseSchema, name='删除技术栈')
    async def delete(self, ids: str = Query(None, description='技术栈id，多条记录以逗号分割')) -> CommonResponseSchema:
        rs = self.ide_stack_service.delete_ide_stack(self.db, ids.split(','))
        if rs:
            return CommonResponseSchema(data={}, message=f'删除成功', success=True)
        else:
            return CommonResponseSchema(data={}, message=f'删除失败', success=False)

    @ide_stack_api.post("/edit", response_model=CommonResponseSchema, name='编辑技术栈')
    async def edit(self, data: IdeStackUpdateRequest = Body(None, description='编辑技术栈实体'),
                   group: Optional[str] = Header(None)) -> CommonResponseSchema:
        data.tenant_id = group
        rs = self.ide_stack_service.update_ide_stack(self.db, data)
        if rs:
            return CommonResponseSchema(data={}, message=f'编辑成功', success=True)
        else:
            return CommonResponseSchema(data={}, message=f'编辑失败', success=False)

    @ide_stack_api.get("/get_ide_stack_options", name='获取技术栈配置下拉选项')
    async def get_ide_environment_options(self,
                                          group: Optional[str] = Header(None)) -> List[OptionsSchema]:
        query = self.ide_stack_service.get_all_ide_stack(self.db, group)

        option_results: List[OptionsSchema] = []
        for row in query:
            option_results.append(OptionsSchema(value=str(row.id), label=row.name))
        return option_results
